import { observable, action } from 'mobx';
import * as API_CENTER from '../apis/center';
import { WeaLocaleProvider } from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;

export class DocCenterStore {
  @observable title = getLabel(132038, '知识中心');
  @observable loading = false;
  @observable numOfAll = {};
  @observable numOfMy = {};
  @observable numOfReplyAndPraise = {};
  @observable rankDepartment = [];
  @observable rankSubDepartment = [];
  @observable rankHead = [];
  @observable lastestReader = [];
  @observable tabInfo = [];
  @observable docList = [];
  @observable viewcondition = '';
  @observable tabInfoOfPartner = [];
  @observable docListOfPartner = [];
  @observable lengthOfPartner = 5;
  @observable hasMorePartner = true;
  @observable userInfo = {
    photoUrl: '',
userDept: 0,
userId: '',
    userName: '',
userSubsection: 0,
  };
  @observable numOfReplyAndPraiseDetail = { hasMore: true };
  @observable numOfReplyAndPraiseDetailData = [];
  @observable uploadSet = {};
  @observable loadingDocList = true;
  @observable loadingDocListPartner = true;
  @observable selectedKeyTabInfo = '0';
  @observable selectedKeyTabInfoOfPartner = '0';
  @observable isFirstLoad = true;
  @observable isFirstLoadPartner = true;
  @observable searchValue = '';
  @observable searchDatas = [];
  @observable categoryDatas=[];
  constructor() {
    this.setTitle = this.setTitle.bind(this);
    this.setLoading = this.setLoading.bind(this);
    this.getNumOfAll = this.getNumOfAll.bind(this);
    this.getNumOfMy = this.getNumOfMy.bind(this);
    this.getNumOfReplyAndPraise = this.getNumOfReplyAndPraise.bind(this);
    this.getRank = this.getRank.bind(this);
    this.getLastestReader = this.getLastestReader.bind(this);
    this.getTabInfo = this.getTabInfo.bind(this);
    this.getDocList = this.getDocList.bind(this);
    this.getTabInfoOfPartner = this.getTabInfoOfPartner.bind(this);
    this.getDocListOfPartner = this.getDocListOfPartner.bind(this);
    this.getUserInfo = this.getUserInfo.bind(this);
    this.accForDoc = this.accForDoc.bind(this);
    this.getNumOfReplyAndPraiseDetail = this.getNumOfReplyAndPraiseDetail.bind(this);
    this.getUploadSet = this.getUploadSet.bind(this);
    this.setSelectedKeyTabInfo = this.setSelectedKeyTabInfo.bind(this);
    this.setSelectedKeyTabInfoOfPartner = this.setSelectedKeyTabInfoOfPartner.bind(this);
    this.setLengthOfPartner = this.setLengthOfPartner.bind(this);
    this.setCategoryDatas = this.setCategoryDatas.bind(this);
  }
  @action

  setTitle(title) {
    this.title = title;
  }
  setLoading(loading) {
    this.loading = loading;
  }
  getNumOfAll(params = {}) {
    API_CENTER.getNumOfAll(params).then((data) => {
      this.numOfAll = data;
    });
  }
  getNumOfMy(params = {}) {
    API_CENTER.getNumOfMy(params).then((data) => {
      this.numOfMy = data;
    });
  }
  getNumOfReplyAndPraise(params = {}) {
    API_CENTER.getNumOfReplyAndPraise(params).then((data) => {
      this.numOfReplyAndPraise = data;
    });
  }
  getRank() {
    API_CENTER.getRank({ rankType: 1 }).then((data) => {
      this.rankDepartment = data;
    });
    API_CENTER.getRank({ rankType: 2 }).then((data) => {
      this.rankSubDepartment = data;
    });
    API_CENTER.getRank({ rankType: 3 }).then((data) => {
      this.rankHead = data;
    });
  }
  getLastestReader(params = {}) {
    API_CENTER.getLastestReader(params).then((data) => {
      this.lastestReader = data.users;
    });
  }
  getTabInfo(params = {}) {
    API_CENTER.getTabInfo(params).then((data) => {
      this.tabInfo = data.groupinfo;
    });
  }
  getDocList(params = { pageSize: 5, viewcondition: '', docsubject: '' }) {
    let localDocList = localStorage.getItem(`docList${this.selectedKeyTabInfo}`);
    if (localDocList) this.docList = JSON.parse(localDocList);
    this.loadingDocList = true;
    API_CENTER.getDocList(params).then((data) => {
      if (this.isFirstLoad) {
        this.docList = data.dataList;
        this.isFirstLoad = false;
      } else if (this.selectedKeyTabInfo === data.viewcondition.toString()) {
          this.docList = data.dataList;
        }
      localStorage.setItem(`docList${data.viewcondition.toString()}`, JSON.stringify(data.dataList));
      this.loadingDocList = false;
    });
  }
  getTabInfoOfPartner(params = {}) {
    API_CENTER.getTabInfoOfPartner(params).then((data) => {
      this.tabInfoOfPartner = data.groupinfo;
    });
  }
  getDocListOfPartner(params = { pageSize: 100, viewcondition: '', docsubject: '' }) {
    let localDocList = localStorage.getItem(`docListOfPartner${this.selectedKeyTabInfo}`);
    if (localDocList && this.selectedKeyTabInfoOfPartner !== '3') this.docListOfPartner = JSON.parse(localDocList);
    this.loadingDocListPartner = true;
    API_CENTER.getDocListOfPartner(params).then((data) => {
      if (this.isFirstLoadPartner) {
        this.docListOfPartner = data.dataList;
        this.isFirstLoadPartner = false;
      } else if (this.selectedKeyTabInfoOfPartner === data.viewcondition.toString()) {
          this.docListOfPartner = data.dataList;
        }
      if (this.docListOfPartner.length <= 5) this.hasMorePartner = false;
      else this.hasMorePartner = true;
      this.lengthOfPartner = 5;
      localStorage.setItem(`docListOfPartner${data.viewcondition.toString()}`, JSON.stringify(data.dataList));
      this.loadingDocListPartner = false;
    });
  }
  getUserInfo(params = {}) {
    API_CENTER.getUserInfo(params).then((data) => {
      this.userInfo = data;
    });
  }
  accForDoc(params = {}) {
    API_CENTER.accForDoc(params).then(() => {});
  }
  getNumOfReplyAndPraiseDetail(params = {}, isClear = false) {
    API_CENTER.getNumOfReplyAndPraiseDetail(params).then((data) => {
      if (isClear) {
        this.numOfReplyAndPraiseDetailData = data.datas;
      } else {
        this.numOfReplyAndPraiseDetailData = this.numOfReplyAndPraiseDetailData.concat(data.datas);
      }
      this.numOfReplyAndPraiseDetail = data;
    });
  }
  getUploadSet(params = {}) {
    API_CENTER.getUploadSet(params).then((data) => {
      this.uploadSet = data.uploadSet;
    });
  }
  setSelectedKeyTabInfo(selectedKeyTabInfo) {
    this.selectedKeyTabInfo = selectedKeyTabInfo;
  }
  setSelectedKeyTabInfoOfPartner(selectedKeyTabInfoOfPartner) {
    this.selectedKeyTabInfoOfPartner = selectedKeyTabInfoOfPartner;
  }
  setLengthOfPartner(lengthOfPartner) {
    this.lengthOfPartner = lengthOfPartner + 5;
    if (this.lengthOfPartner >= this.docListOfPartner.length) this.hasMorePartner = false;
  }

  setCategoryDatas(categoryDatas) {
    this.categoryDatas = categoryDatas;
  }
}

